﻿using System;
using System.Drawing;
using XFControls.Utils;
using XFControls.XFPanels.XFPanelItems;
using System.Windows.Forms;

namespace XFControls.XFPanels.XFPanelModals
{
    public class XFModalButton : IXFModelButton
    {
        private int _height;
        private int _width;

        public XFModalButton()
        {
            Style = new XFItemStyle
                    {
                        TextFont = new Font(FontFamily.GenericSansSerif, 11, FontStyle.Bold),
                        TextColor = Color.White,
                        BackgroundColor = Color.RoyalBlue,
                        SelectedBackgroundColor = Color.Black,
                        Padding = 20
                    };
        }

        #region IXFModelButton Members

        public string Text { get; set; }
        public bool Selected { get; set; }
        public XFItemStyle Style { get; set; }

        public virtual XFModalClickResult GetClickResult()
        {
            throw new NotImplementedException();
        }

        public void ItemPaint(Graphics g, int x, int y)
        {
            SizeF mes = g.MeasureString(Text, Style.TextFont);
            _width = (int)mes.Width + Style.PaddingLeft + Style.PaddingRight;
            _height = (int)mes.Height + Style.PaddingTop + Style.PaddingBottom;

            using (var b = new SolidBrush(Selected ? Style.SelectedBackgroundColor : Style.BackgroundColor))
                g.FillRectangle(b, x, y, _width, _height);

            using (var b = new SolidBrush(Style.TextColor))
                g.DrawString(Text, Style.TextFont, b, x + Style.PaddingLeft, y + Style.PaddingTop);
        }

        public int GetHeight()
        {
            if (_height > 0)
                return _height;

            Size mes = Style.TextFont.MeasureString(Text, new Rectangle(0, 0, Screen.PrimaryScreen.WorkingArea.Width, 0));
            _width = mes.Width + Style.PaddingLeft + Style.PaddingRight;
            _height = mes.Height + Style.PaddingTop + Style.PaddingBottom;

            return GetHeight();
        }

        public void ResetHeight()
        {
            _height = 0;
        }

        public void Dispose()
        {
            if (Style != null)
                Style.Dispose();
        }

        #endregion

        public int GetWidth()
        {
            if (_width > 0)
                return _width;

            GetHeight();
            return GetWidth();
        }
    }

    public class XFModalYesButton : XFModalButton
    {
        public XFModalYesButton()
        {
            Text = "Yes";
        }

        public override XFModalClickResult GetClickResult()
        {
            return XFModalClickResult.Yes;
        }
    }

    public class XFModalNoButton : XFModalButton
    {
        public XFModalNoButton()
        {
            Text = "No";
        }

        public override XFModalClickResult GetClickResult()
        {
            return XFModalClickResult.No;
        }
    }

    public class XFModalCancelButton : XFModalButton
    {
        public XFModalCancelButton()
        {
            Text = "Cancel";
        }

        public override XFModalClickResult GetClickResult()
        {
            return XFModalClickResult.Cancel;
        }
    }
}